package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	go watch(ctx, "[Monitor1]")
	go watch(ctx, "[Monitor2]")
	go watch(ctx, "[Monitor3]")

	time.Sleep(10 * time.Second)
	fmt.Println("notify monitor stop....")
	cancel()
	time.Sleep(10 * time.Second)
}

func watch(ctx context.Context, name string) {
	for {
		select {
		case <-ctx.Done():
			fmt.Println(name, "has been stopped")
			return
		default:
			job(name)
		}
	}
}

func job(name string) {
	fmt.Println(name, " is watching.....")
	time.Sleep(2 * time.Second)
}
